﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using NanoERP.Business.BaseInfo;
using NanoERP.Common;
using NanoERP.Model;

namespace NanoERP
{
    public partial class Form1 : Form
    {
        readonly AccountsubjectBLL _accountsubjectBLL;
        public Form1()
        {
            InitializeComponent();

            _accountsubjectBLL = new AccountsubjectBLL();
            QueryInfo();
        }

        void QueryInfo()
        {
            treeList.Nodes.Clear();
            treeList.BeginUnboundLoad();
            var list = _accountsubjectBLL.Query(0);
            list.ForEach(r =>
                {
                    var tgn = treeList.AppendNode(new object[] { r.Subjectno, r.Subjectname, r.Subjectpym, r.Begininmoney, r.Beginoutmoney, r.Beginmoney, r.Inmoney, r.Outmoney, r.Endmoney, r.Isstandard, r.Note }, null);
                    tgn.Tag = r;

                    System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        var listSec = _accountsubjectBLL.Query(r.Id);
                        RecursiveLoopNodes(listSec, tgn);
                    });
                });

            treeList.EndUnboundLoad();
        }

        /// <summary>
        /// 递归循环调用
        /// </summary>
        /// <param name="firList">一级树集合</param>
        /// <param name="pNode">上级节点</param>
        void RecursiveLoopNodes(List<Accountsubject> firList, TreeListNode pNode)
        {
            firList.ForEach(r =>
            {
                var thiNode = treeList.AppendNode(new object[] { r.Subjectno, r.Subjectname, r.Subjectpym, r.Begininmoney, r.Beginoutmoney, r.Beginmoney, r.Inmoney, r.Outmoney, r.Endmoney, r.Isstandard, r.Note }, pNode);
                thiNode.Tag = r;
                var listSec = _accountsubjectBLL.Query(r.Id);
                if (listSec.Count > 0)
                    RecursiveLoopNodes(listSec, thiNode);
            });
        }

        private void treeList_AfterExpand(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            treeListColumn1.BestFit();
        }

        private IEnumerable<TreeListNode> _treeNodes;
        private int _nodeIndex;
        private void button1_Click(object sender, System.EventArgs e)
        {
            if (_treeNodes == null || !_treeNodes.Any()) return;
            _treeNodes.ToArray()[_nodeIndex].Selected = true;
            _nodeIndex = (_nodeIndex < _treeNodes.Count() - 1) ? ++_nodeIndex : 0;

        }

        private void txtKey_TextChanged(object sender, EventArgs e)
        {
            _nodeIndex = 0;
            if (_nodeIndex == 0)
                _treeNodes = treeList.Nodes.TreeWhere(t =>
                    {
                        var acc = t.Tag as Accountsubject;
                        var regex = string.Format("{0}|{1}{2}", acc.Subjectno, acc.Subjectname, acc.Subjectpym);
                        return Regex.IsMatch(regex, @"(?i)" + txtKey.Text);
                    });
        }
    }

}
